﻿using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using SystemsBiologyWorkbench = SBW;

namespace Athena.SBW
{
    public class SBWmenu
    {
        //Threaded to avoid SBW menu freezing

        #region SBW menu integration

        public static ToolStripMenuItem SBWMenuItem;

        public static void UpdateSBWMenu(ToolStripMenuItem mnuSBW)
        {
            try
            {
                SBWMenuItem = new ToolStripMenuItem();
                Thread sbwmenu = new Thread(new ParameterizedThreadStart(updateSBWMenu));
                sbwmenu.Start(SBWMenuItem);
                sbwmenu.Join(10000);                
                int n = SBW.SBWmenu.SBWMenuItem.DropDownItems.Count;
                for (int i = 0; i < n; ++i)
                    mnuSBW.DropDownItems.Add(SBW.SBWmenu.SBWMenuItem.DropDownItems[0]);
            }
            catch (Exception)
            {
                //mnuSBW.Visible = false;
                //mnuSBW.Enabled = false;
            }
            SBWMenuItem = null;
        }

        class SBWAnalyzer
        {
            private string _sModuleName;

            public string ModuleName
            {
                get { return _sModuleName; }
                set { _sModuleName = value; }
            }
            private string _sService;

            public string ServiceName
            {
                get { return _sService; }
                set { _sService = value; }
            }
            public SBWAnalyzer(string sModule, string sService)
            {
                _sModuleName = sModule;
                _sService = sService;
            }

        }
        static Hashtable oAnalyzers;
        private static string _IgnoredInSBWMenu = "Athena";
        public static string IgnoredInSBWMenu
        {
            get
            {
                return _IgnoredInSBWMenu;
            }
            set
            {
                _IgnoredInSBWMenu = value;
            }
        }


        static void updateSBWMenu(object oSBWMenuObject)
        {
            try
            {
                ToolStripMenuItem oSBWMenu = (ToolStripMenuItem)oSBWMenuObject;

                bool bWasConnected = SystemsBiologyWorkbench.LowLevel.SBWGetConnectionStatus();
                if (!bWasConnected)
                    SystemsBiologyWorkbench.LowLevel.SBWConnect();

                oSBWMenu.DropDownItems.Clear();
                oAnalyzers = new Hashtable();
                SystemsBiologyWorkbench.Module oBroker = new SystemsBiologyWorkbench.Module("BROKER");
                SystemsBiologyWorkbench.Service oService = oBroker.getService("BROKER");
                SystemsBiologyWorkbench.Method oMethod = oService.getMethod("{}[] findServices(string,boolean)");
                ArrayList[] oList = oMethod.Call("/Analysis", true).get1DListArray();

                SortedList oModules = new SortedList(CaseInsensitiveComparer.Default);

                foreach (ArrayList list in oList)
                {
                    try
                    {
                        oModules.Add((string)list[2], list);
                    }
                    catch (Exception)
                    {
                    }
                }

                foreach (DictionaryEntry entry in oModules)
                {
                    //string sKey = (string)entry.Key;
                    ArrayList list = (ArrayList)entry.Value;
                    if (_IgnoredInSBWMenu != (string)list[0])
                    {
                        ToolStripButton oItem = new ToolStripButton((string)list[2]);

                        oAnalyzers.Add((string)list[2], new SBWAnalyzer((string)list[0], (string)list[1]));

                        oItem.Click += new EventHandler(oItem_Click);
                        oSBWMenu.DropDownItems.Add(oItem);
                    }
                }

                if (!bWasConnected)
                    SystemsBiologyWorkbench.LowLevel.SBWDisconnect();
            }
            catch (Exception)
            {
            }
        }


        static void oItem_Click(object sender, EventArgs e)
        {
            try
            {
                string sSBML = Utility.GetSBML();
                if ((sSBML == null) || sSBML == "")
                {
                    MessageBox.Show("There is no model to analyze. Load a model first.", "No Model loaded", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                SBWAnalyzer oTemp = (SBWAnalyzer)oAnalyzers[((ToolStripButton)sender).Text];
                SystemsBiologyWorkbench.Module oModule = new SystemsBiologyWorkbench.Module(oTemp.ModuleName);
                SystemsBiologyWorkbench.Service oService = oModule.getService(oTemp.ServiceName);
                oService.getMethod("void doAnalysis(string)").Send(sSBML);
            }
            catch (SystemsBiologyWorkbench.SBWException ex)
            {
                MessageBox.Show("An error while calling the Analyzer." + Environment.NewLine
                    + Environment.NewLine
                    + Environment.NewLine + "(" + ex.Message + Environment.NewLine + ex.DetailedMessage + ")",
                    "Error calling the Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
            }

        }

        #endregion
    }
}
